﻿using MediatR;
using S.Organization.Domian.dto;
using S.Organization.Domian.Model;
using S.Organization.Infrastructure;
using S.Organization.Read.API.Application.Command.FieldSettingCommand;
using S.Organization.Read.API.Application.Command.SheepFarmCommand;

namespace S.Organization.Read.API.Application.CommandHandler.FieldSettingCommandHandler
{
    public class GetFieldSettingCommandHandler : IRequestHandler<GetFieldSettingCommand, List<DTO>>
    {
        private readonly IBaseRepository<FieldSetting> _repository;
        private readonly IBaseRepository<CottageSetting> rep;

        public GetFieldSettingCommandHandler(IBaseRepository<FieldSetting> repository, IBaseRepository<CottageSetting> rep)
        {
            _repository = repository;
            this.rep = rep;
        }

        public async Task<List<DTO>> Handle(GetFieldSettingCommand request, CancellationToken cancellationToken)
        {
            var list = await _repository.AllAsync();
            var  list1= await rep.AllAsync();
            var list2 = list1.Where(x => x.CottageName == request.Id).FirstOrDefault();
            int name = list2.CottageId;
            var res=list.Where(x=>x.CottageId== name).ToList();

            List<DTO> result = new List<DTO>(); 
            foreach (var item in res)
            {
                DTO data = new DTO();
                data.Id = item.FieldId;
                data.Name = item.FieldName;
                result.Add(data);
            }

            return result;

        }
    }
}
